import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getCurrentUserInfo } from '@/api/app'
import store from './store'
import { Message } from 'element-ui'
import { AesDecrypt } from './utils/AesDecrypt'

NProgress.configure ({
  showSpinner: false
})
let first = false
router.beforeEach (async(to, from, next) => {
  NProgress.start ()
  if (!store.state.currentUser.fax) {
    if (to.path === '/performanceMenu' || to.path === '/depperformanceMenu' || to.path === '/cokpitMenu' || to.path === '/personInfoManage') {
      next ('/home')
    }
    try {
      store.state.permissionList = []
      const { data } = await getCurrentUserInfo ()
      let result = AesDecrypt (data.result)
      store.state.userId = result.userId
      store.state.userName = result.userName
      store.state.userCode = result.userCode
      store.state.deptId = result.deptId
      store.state.deptName = result.deptName
      store.state.permissionList = result.permissionList
      console.log ('数组' + '==============' + store.state.permissionList)
      next ()
    } catch (err) {
      return Message ({
        type: 'error',
        dangerouslyUseHTMLString: true,
        message: '获取当前登陆人失败，请联系管理员'
      })
      NProgress.done ()
    }
  } else {
    next ()
  }
  first = true

})

router.afterEach (() => {

  NProgress.done ()
})
